package service.impl.v2;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import dao.v2.ConfigVOMapper;
import dao.v2.MemberVOMapper;
import dao.v2.PromotionVOMapper;
import model.MemberVO;
import model.PromotionGoodVO;
import model.PromotionItemVO;
import model.PromotionVO;
import service.v2.CacheManagerService;
import service.v2.GoodService;
import service.v2.PromotionService;

@Service
public class PromotionServiceImpl implements PromotionService {
	
	@Autowired
	private PromotionVOMapper dao;
	@Autowired
	private GoodService goodService;
	@Autowired
	private MemberVOMapper memberDao;
	@Autowired
	private ConfigVOMapper configVOMapper;
	@Autowired
	private CacheManagerService cacheManagerService;
	
	@Override
	public PromotionGoodVO getListByGood(String good_id, MemberVO memberVO, String lang) throws Exception {
		PromotionGoodVO promotionGoodVO =  dao.getListByGood(good_id);
		
		if(promotionGoodVO != null && promotionGoodVO.getPromotion() != null){
			String promotion_item = "";
			PromotionVO promotionVO = promotionGoodVO.getPromotion();
			boolean flag = goodService.promotionShow(memberVO, promotionVO.getMax_level());
			if(flag){
				for (int i=0; i<promotionGoodVO.getPromotion().getItemList().size(); i++) {
					PromotionItemVO itemVO = promotionVO.getItemList().get(i);
					if(lang==null){
						if(i > 0){
							promotion_item += "，";
						}else{
							promotion_item += "[满"+("0".equals(promotionVO.getReach_type())?"额":"件")+"减]";
						}
						promotion_item += goodService.promotionItem(itemVO, promotionVO);
					}else{
						if(i > 0){
							promotion_item += "，";
						}
						promotion_item += goodService.promotionItemEn(itemVO, promotionVO);
					}
				}
			}
			promotionGoodVO.setPromotion_item(promotion_item);
			return promotionGoodVO;
		}else{
			return new PromotionGoodVO();
		}
	}

	@Override
	public List<PromotionItemVO> selectPromotionItemList(String promotion_id) {
		return dao.selectPromotionItemList(promotion_id);
	}

	@Override
	public String getContenStr(String promotion_id, String member_id) {
		String contentStr="";
		
		List<PromotionItemVO> list=dao.getPromotionItemList1(promotion_id);
        DecimalFormat df = new DecimalFormat("#");  

		for(int i=0;i<list.size();i++){
			if("1".equals(list.get(i).getType())){
				//钱
				if("0".equals(list.get(i).getReach_type())){
					if(member_id !=null){
						String cartPromotionGoodsCash =dao.getCartPromotionGoodsCash(promotion_id,member_id);
						double cash =Double.parseDouble(cartPromotionGoodsCash);
						Double obj1 = new Double(cash);  
	
						double reach =Double.parseDouble(list.get(i).getReach());
						Double obj2 = new Double(reach);  
						int retval =  obj1.compareTo(obj2); 
						if(retval>0){
							if(i==list.size()-1){
								if("1".equals(list.get(i).getCountry())){
									if("Y".equals(list.get(i).getPromotion_freight())){
										contentStr="已满足【满"+list.get(i).getReach()+"元减"+list.get(i).getReduce_cash()+"元包邮】";
									}else{
										contentStr="已满足【满"+list.get(i).getReach()+"元减"+list.get(i).getReduce_cash()+"元】";
									}
								}else{
									contentStr="You've already saved US $"+list.get(i).getReduce_cash();
								}
								break;
							}
						}else if(retval < 0) {  
					    	BigDecimal b1 = new BigDecimal(cash);
					    	BigDecimal b2 = new BigDecimal(reach);
					    	Double need_cash= b2.subtract(b1).doubleValue();
					    	if("1".equals(list.get(i).getCountry())){
					    		if("Y".equals(list.get(i).getPromotion_freight())){
						    		contentStr="再购"+df.format(need_cash)+"元立享"+"【满"+list.get(i).getReach()+"元减"+list.get(i).getReduce_cash()+"元包邮】";
						    	}else{
						    		contentStr="再购"+df.format(need_cash)+"元立享"+"【满"+list.get(i).getReach()+"元减"+list.get(i).getReduce_cash()+"元】";
						    	}
					    	}else{
					    		contentStr="Spend another US $"+df.format(need_cash)+" to get US $"+list.get(i).getReduce_cash()+" off.";
					    	}
					    	break;
					    }else {  
					    	if(i==list.size()-1){
					    		if("1".equals(list.get(i).getCountry())){
					    			if("Y".equals(list.get(i).getPromotion_freight())){
							    		contentStr="已满足【满"+list.get(i).getReach()+"元减"+list.get(i).getReduce_cash()+"元包邮】";
							    	}else{
							    		contentStr="已满足【满"+list.get(i).getReach()+"元减"+list.get(i).getReduce_cash()+"元】";
							    	}
					    		}else{
					    			contentStr="You've already saved US $"+list.get(i).getReduce_cash();
					    		}
						    	break;
							}
					    }  
					}else{
						if(i==0){//没登录时
							if("1".equals(list.get(i).getCountry())){
								if("Y".equals(list.get(i).getPromotion_freight())){
									contentStr="再购"+list.get(i).getReach()+"元立享"+"【满"+list.get(i).getReach()+"元减"+list.get(i).getReduce_cash()+"元包邮】";
								}else{
									contentStr="再购"+list.get(i).getReach()+"元立享"+"【满"+list.get(i).getReach()+"元减"+list.get(i).getReduce_cash()+"元】";
								}
							}else{
								contentStr="Spend another US $"+list.get(i).getReach()+" to get US $"+list.get(i).getReduce_cash()+" off.";
							}
							
						}
					}
				}
				//件
				if("1".equals(list.get(i).getReach_type())){
					if(member_id !=null){
						String count = dao.getCartPromotionCount(promotion_id,member_id);
						if(Integer.parseInt(count)>=Integer.parseInt(list.get(i).getReach())){
							if(i==list.size()-1){
								if("1".equals(list.get(i).getCountry())){
									if("Y".equals(list.get(i).getPromotion_freight())){
										contentStr="已满足【满"+list.get(i).getReach()+"件减"+list.get(i).getReduce_cash()+"元包邮】";
									}else{
										contentStr="已满足【满"+list.get(i).getReach()+"件减"+list.get(i).getReduce_cash()+"元】";
									}
								}else{
									contentStr="You've already saved US $"+list.get(i).getReduce_cash();
								}
								break;
							}
						}
						if(Integer.parseInt(count)<Integer.parseInt(list.get(i).getReach())){
							int n= Integer.parseInt(list.get(i).getReach())-Integer.parseInt(count);
							if("1".equals(list.get(i).getCountry())){
								if("Y".equals(list.get(i).getPromotion_freight())){
									contentStr="再购"+n+"件立享"+"【满"+list.get(i).getReach()+"件减"+list.get(i).getReduce_cash()+"元包邮】";

								}else{
									contentStr="再购"+n+"件立享"+"【满"+list.get(i).getReach()+"件减"+list.get(i).getReduce_cash()+"元】";
								}
							}else{
								contentStr="Buy another "+n+" piece(s) to get $"+list.get(i).getReduce_cash()+" off.";
							}
							break;
						}
					}else{
						if(i==0){//没登录时
							if("1".equals(list.get(i).getCountry())){
								if("Y".equals(list.get(i).getPromotion_freight())){
									contentStr="再购"+list.get(i).getReach()+"件立享"+"【满"+list.get(i).getReach()+"件减"+list.get(i).getReduce_cash()+"元包邮】";
								}else{
									contentStr="再购"+list.get(i).getReach()+"件立享"+"【满"+list.get(i).getReach()+"件减"+list.get(i).getReduce_cash()+"元】";
								}
							}else{
								contentStr="Buy another "+list.get(i).getReach()+" piece(s) to get $"+list.get(i).getReduce_cash()+" off.";
							}
						}
					}
				}
			}
			
		}
		
		return contentStr;
	}
		
}
